package com.mall.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.SmsFlashPromotionApi;
import com.mall.exception.ConsumerException;
import com.mall.pageResult.PageResult;
import com.mall.pojo.SmsFlashPromotion;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FlashService {

    @DubboReference
    private SmsFlashPromotionApi promotionApi;

    /**
     * 根据活动名称分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult<SmsFlashPromotion> findByPageAndCondition(Integer pageNum, Integer pageSize, String keyword) {
        //根据条件分页查询秒杀活动
        Page<SmsFlashPromotion> page = promotionApi.findByPageAndCondition(pageNum, pageSize, keyword);
        //组装返回结果数据
        PageResult<SmsFlashPromotion> result = new PageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(page.getRecords());
        //result.setTotalPage(Convert.toInt(Math.ceil(promotions.size() / pageSize))); //向上取整
        result.setTotalPage(Convert.toInt(page.getPages()));
        result.setTotal(Convert.toInt(page.getTotal()));
        return result;
    }

    /**
     * 编辑活动
     *
     * @param id
     * @param flashPromotion //对象的属性可能为空
     * @return
     */
    public Integer update(Integer id, SmsFlashPromotion flashPromotion) {
        //判断活动开始结束时间严谨性
        Date endDate = flashPromotion.getEndDate();
        Date startDate = flashPromotion.getStartDate();

        if (ObjectUtil.isNotNull(endDate) && ObjectUtil.isNotNull(startDate)) {
            if (endDate.getTime() - startDate.getTime() <= 0) {
                throw new ConsumerException("活动时间设置错误");
            }
        }

        //编辑活动信息
        if (ObjectUtil.isNull(flashPromotion.getId())) {
            flashPromotion.setId(Convert.toLong(id));
        }

        Integer count = promotionApi.update(flashPromotion);
        return count;
    }

    /**
     * 添加活动
     *
     * @param flashPromotion
     */
    public Integer create(SmsFlashPromotion flashPromotion) {
        //判断活动开始结束时间严谨性
        Date endDate = flashPromotion.getEndDate();
        Date startDate = flashPromotion.getStartDate();

        if (ObjectUtil.isNotNull(endDate) && ObjectUtil.isNotNull(startDate)) {
            if (endDate.getTime() - startDate.getTime() <= 0) {
                throw new ConsumerException("活动时间设置错误");
            }
        }

        //添加活动信息
        flashPromotion.setCreateTime(new Date(System.currentTimeMillis()));
        Integer count = promotionApi.create(flashPromotion);
        return count;
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    public Integer delete(Integer id) {
        //根据id查询活动信息
        SmsFlashPromotion promotion = promotionApi.findById(id);
        //上线的活动不可被删除
        if (promotion.getStatus() == 1) {
            throw new ConsumerException("上线活动不可被删除");
        }
        Integer count = promotionApi.delete(id);
        return count;
    }

    /**
     * 修改活动上下线状态
     *
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Integer id, Integer status) {
        //根据id查询活动信息
        SmsFlashPromotion promotion = promotionApi.findById(id);
        //修改活动上下线状态
        promotion.setStatus(status);
        Integer count = promotionApi.update(promotion);
        return count;
    }
}
